#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMap>
#include <QDebug>
#include <QModbusClient>
#include <QModbusDataUnit>
#include <QModbusTcpClient> // 如果使用TCP连接
#include <QModbusRtuSerialMaster> // 如果使用串口连接
#include <QTimer>
#include <QTime>
#include <QDateTime>
#include <form.h>
#include <QThread>
#include <t_connect.h>
#include <registers.h>
#include <QMessageBox>



QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    bool Write_modbus_tcp_Coils(int star_add, int number, QString str1);
    bool Write_modbus_tcp_HoldingRegisters( QString str,int star_add, int number);

    void is_setenabel_yes();
    void is_setenabel_no();

    QModbusTcpClient * get_agv_client();
    bool get_agv_state();

signals:
    void statechange_on();
    void statechange_off();

private slots:
    void onStateChanged(QModbusTcpClient *client);
    void slot_timeout(void);
    void on_add_button_clicked();
    void on_del_button_clicked();
    void on_clear_button_clicked();

    void from_add_agv(QString agv_name,QString ip,int port);

    void on_start_ctrl_move_clicked();

    void on_stop_ctrl_move_clicked();

    void on_charge_clicked();

    void on_stop_charge_clicked();

    void on_local_clicked();

    void on_stop_local_clicked();

    void on_pause_button_clicked();

    void on_continue_button_clicked();

    void on_connect_pushButton_clicked();


    void on_disconnect_pushButton_clicked();

    void on_cancel_button_clicked();

    void on_station_move_clicked();

private:
    Ui::Widget *ui;
    QString ip;
    int port;
    QTimer * timer;
    QTimer * move_timer;

    Form * add;

    int count = 0;
    bool connect_state = false;
    bool hand_ctrl_move = false;
    bool is_charge = true;
    bool is_local = true;


    struct agv_struct{
        QString ip;
        int port;
        bool state;
        QModbusTcpClient * client;
    };
    struct agv_struct agv_info;
    QMap<QString,agv_struct> client_map;

    int y = 0;
};
#endif // WIDGET_H
